<?php
namespace app\demo\controller;

use think\Db;
use yunj\Controller;

class FormDemo extends Controller {

    public function demo(){
        $builder=YF('demo')
            ->tab(['base'=>'基础'])
            ->field(function ($tab){
                $field=[
                    // 单上传/预览
                    'img_test'=>[
                        'title'=>'单图',
                        'type'=>'img',
                    ],
                    // 多图上传/预览
                    'imgs_test'=>[
                        'title'=>'多图',
                        'type'=>'imgs',
                        'max'=>3,
                    ],
                    // 多文件上传/预览
                    'files_test'=>[
                        'title'=>'多文件',
                        'type'=>'files',
                        'max'=>3,
                    ],
                    // 下拉搜索
                    'dropdown_search_test'=>[
                        'title'=>'下拉搜索',
                        'type'=>'dropdown_search',
                        'value'=>[25224,25225],
                        'verify'=>'require|array',
                        'url'=>url('dropdownSearchOptions'),
                        //'multi'=>false,
                        //'disabled'=>true,
                        'desc'=>'下拉搜索提示语',
                    ],
                    // tree
                    "tree_test"=>[
                        'title'=>'树',
                        'type'=>'tree',
                        //'mode'=>'radio',
                        'all_optional'=>true,
                        'nodes'=>[
                            ["id"=>1,"pid"=>0,"name"=>"测试1","level"=>0],
                            ["id"=>2,"pid"=>0,"name"=>"测试2","level"=>0],
                            ["id"=>3,"pid"=>1,"name"=>"测试3","level"=>1],
                        ],
                        //'value'=>[25224,25225],
                        //'verify'=>'require|array',
                        //'disabled'=>true,
                        'desc'=>'树提示语',
                    ],
                    // 地区联动
                    'area_test'=>[
                        'title'=>'地区',
                        'type'=>'area',
                        //'acc'=>'district',
                        //'value'=>['province'=>'210000','city'=>'210100','district'=>'210102'],
                        //'disabled'=>true,
                        'desc'=>'地区提示语',
                    ],
                    "markdown_test"=>[
                        'title'=>'Markdown',
                        'type'=>'markdown',
                        "value"=>"### 123",
                        //'readonly'=>true,
                        'desc'=>'markdown提示语',
                    ],
                ];
                return $field;
            })
            ->button(['reset','submit'])
            ->submit(function ($data,$tab_data){
                // 简单的表单提交结果
                dump($data);
                // 多tab表单结果
                dump($tab_data);

                // ...业务代码

                // 返回相应结果
                return success_json();
            });
        $builder->assign($this);
        return $this->fetch();
    }

    public function dropdownSearchOptions(){
        $data=input('get.');
        // 输入关键字
        $keywords=filter_sql($data['keywords']);
        // 使用 , 分割的初始值
        $ids=filter_sql($data['ids']);
        // 进行数据查询
        $whereArr=[];
        if($keywords) $whereArr[]=['name_cn|name_en','like',$keywords.'%'];
        if($ids) $whereArr[]=['id','in',$ids];
        $fieldArr=['id','name_cn','name_en'];
        $datas=Db::name('example')->field($fieldArr)->where($whereArr)->order(['id'=>'desc'])->limit(0,20)->select();
        $items=[];
        foreach ($datas as $data){
            $items[$data['id']]="{$data['name_cn']}（{$data['name_en']}）";
        }
        // 返回数据
        return success_json($items);
    }

}